package cn.edu.nuc.bre.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.edu.nuc.bre.dao.QueryMapper;
import cn.edu.nuc.bre.dao.TransDetailMapper;
import cn.edu.nuc.bre.dto.QueryHandler;
import cn.edu.nuc.bre.entity.TransDetail;
import cn.edu.nuc.bre.service.interfaces.TransDetailService;
import cn.edu.nuc.bre.util.PageInfo;
import cn.edu.nuc.bre.util.ValidateUtil;

/**
 * 事务明细业务实现
 * 
 * @author 王凯
 *
 */
@Service
public class TransDetailServiceImpl implements TransDetailService {

	/**
	 * 事务明细DAO
	 */
	@Autowired
	private TransDetailMapper transDetailMapper;

	/**
	 * 查询管理器
	 */
	@Autowired
	private QueryMapper queryMapper;

	/**
	 * 添加事务明细
	 */
	@Override
	public boolean add(TransDetail one) {

		one.setTdstate(1);
		if (transDetailMapper.insertSelective(one) == 1) { //插入成功
			return true;
		} 
		return false;
	}

	/**
	 * 删除（禁用）事务明细
	 */
	@Override
	public boolean delete(Integer tdid) {

		//设置好id和状态码（状态码为0表示删除）
		TransDetail one = new TransDetail();
		one.setTdid(tdid);
		one.setTdstate(0);
		
		//执行更新操作
		if (transDetailMapper.updateByPrimaryKeySelective(one) == 1) {
			return true;
		} 
		
		//失败返回false
		return false;
	}

	/**
	 * 按Id查询事务明细
	 */
	@Override
	public TransDetail findById(Integer tdid) {
		return transDetailMapper.selectByPrimaryKey(tdid);
	}

	/**
	 * 分页查询事务明细信息
	 */
	@Override
	public PageInfo<TransDetail> findByQueryHandler(Integer targetPage, String transDetailName, String transTypeName) {
		
		// 设置查询条件查询明细(要求必须是未删除的明细，每页查询5条记录)
		QueryHandler queryHandler = QueryHandler.creat().andColumnMark("tdstate", "!=", "0");
		
		//设置模糊查询的条件
		if (ValidateUtil.isNotNullOrSpace(transDetailName) 
				&& ValidateUtil.isNotNullOrSpace(transTypeName)) {
			
			queryHandler.andColumnLike("tdname", transDetailName);
			queryHandler.andColumnLike("ttname", transTypeName);
		} else if (ValidateUtil.isNotNullOrSpace(transDetailName) 
				&& !ValidateUtil.isNotNullOrSpace(transTypeName)) {
			
			queryHandler.andColumnLike("tdname", transDetailName);
		} else if(ValidateUtil.isNotNullOrSpace(transTypeName)
				&& !ValidateUtil.isNotNullOrSpace(transDetailName)) {
			
			queryHandler.andColumnLike("ttname", transTypeName);
		}
		
		queryHandler.OrderByDESC("tdid");
		queryHandler.limit(targetPage, 5);
		queryHandler.set(); //查询之前要执行set方法生成查询用的sql语句
		
		//查询得到结果集
		List<TransDetail> list = queryMapper.queryTransDetailPro(queryHandler);
		
		//根据查询条件设置分页信息
		long rootSize = 0;
		int nowPage = 1;
		int pageSize = 5;
		if (queryHandler == null || queryHandler.getLimitSentence() == null) {// 没分页
			if (list != null) {
				rootSize = list.size();
			}
		} else {
			nowPage = queryHandler.getPageNumber();
			pageSize = queryHandler.getPageSize();
			rootSize = queryMapper.findLastRows();
		}
		
		//封装并返回结果集
		PageInfo<TransDetail> pi = new PageInfo<TransDetail>((int) rootSize, nowPage, pageSize);
		pi.setBasePageList(list);
		return pi;
	}

	/**
	 * 修改事务明细
	 */
	@Override
	public boolean update(TransDetail transdetail) {

		if (transDetailMapper.updateByPrimaryKeySelective(transdetail) == 1) {
			return true;
		}
		return false;
	}
	
	/**
	 * 按照事务类型查询对应的明细
	 */
	public List<TransDetail> findDetailsByType(Integer ttypeid) {
		
		return transDetailMapper.selectByType(ttypeid);
	}

}
